package cn.szzsi;

/**
 * Created by weimin28 on 2017/2/20.
 */

import cn.szzsi._admin._AdminRoutes;
import cn.szzsi._sys._SysRoutes;
import cn.szzsi._web._WebRoutes;
import cn.szzsi._webpc._WebPcRoutes;
import cn.szzsi.api.controller._ApiRoutes;
import cn.szzsi.api.service.ApiService;
import cn.szzsi.common.kit.EHcacheUtil;
import cn.szzsi.common.kit.SerialNumberUtil;
import cn.szzsi.common.model._MappingKit;
import cn.szzsi.common.model.zshop.PlateformConfig;
import cn.szzsi.common.model.zshop._ZshopMappingKit;
import cn.szzsi.core.CommonRoutes;
import cn.szzsi.core.CoreConfig;
import cn.szzsi.core.MyProp;
import cn.szzsi.core.dto.Myexception;
import cn.szzsi.core.file.Files;
import cn.szzsi.core.interceptor.ExceptionInterceptor;
import cn.szzsi.core.kit.CommonUtil;
import cn.szzsi.core.plugin.dubbo.DubboPlugin;
import cn.szzsi.mallservice.ApiInterface;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.alibaba.dubbo.config.RegistryConfig;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.json.MixedJsonFactory;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.template.Engine;
import org.beetl.ext.jfinal3.JFinal3BeetlRenderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Config
 */
public class _Config extends JFinalConfig {
	private static final Logger LOG = LoggerFactory.getLogger(_Config.class);
	public static MyProp p = CoreConfig.p;

	//private static Prop p = loadConfig();
	private WallFilter wallFilter;
	public static ExecutorService fixedThreadPool;
	/**
	 * 启动入口，运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 *
	 * 使用本方法启动过第一次以后，会在开发工具的 debug、run configuration 中自动生成
	 * 一条启动配置项，可对该自动生成的配置再继续添加更多的配置项，例如 VM argument 可配置为：
	 * -XX:PermSize=64M -XX:MaxPermSize=256M
	 * 上述 VM 配置可以缓解热加载功能出现的异常
	 */
	public static void main(String[] args) {
		/**
		 * 特别注意：Eclipse 之下建议的启动方式
		 */
//		JFinal.start("src/main/webapp", 8080, "/", 5);

		/**
		 * 特别注意：IDEA 之下建议的启动方式，仅比 eclipse 之下少了最后一个参数
		 */
		JFinal.start("src/main/webapp", 8096, "/");
	}
	@Override
	public void configConstant(Constants me) {
		me.setErrorView(401, "/html/401.html");
		me.setErrorView(403, "/html/403.html");
		me.setError404View("/html/404.html");
		me.setError500View("/html/500.html");

		// beetl jfinal2集成
//        me.setMainRenderFactory(new BeetlRenderFactory());
		//beetl jfinal3集成
		JFinal3BeetlRenderFactory rf = new JFinal3BeetlRenderFactory();
		rf.config();
		me.setRenderFactory(rf);
		// 设定为开发者模式
		me.setDevMode(true);
		me.setJsonFactory(MixedJsonFactory.me());
	}

	/**
	 * 路由拆分到 FrontRutes 与 AdminPlatformRoutes 之中配置的好处：
	 * 1：可分别配置不同的 baseViewPath 与 Interceptor
	 * 2：避免多人协同开发时，频繁修改此文件带来的版本冲突
	 * 3：避免本文件中内容过多，拆分后可读性增强
	 * 4：便于分模块管理路由
	 */
	public void configRoute(Routes me) {
		me.add(new _AdminRoutes());//商户后台
		me.add(new _WebRoutes());//商户前台(mobile)
		me.add(new _WebPcRoutes());//商户前台(pc)
		me.add(new _ApiRoutes());//api接口
		me.add(new CommonRoutes());//公用
		me.add(new _SysRoutes());//超级管理员
	}
	/**
	 * 抽取成独立的方法，例于 _Generator 中重用该方法，减少代码冗余
	 */
	public static DruidPlugin getDruidPlugin() {
		return new DruidPlugin(p.get("mallJdbcUrl"), p.get("user"), p.get("password").trim());
	}
	/**
	 * 配置模板引擎，通常情况只需配置共享的模板函数
	 */
	public void configEngine(Engine me) {

	}

	public void beforeJFinalStop(){
		fixedThreadPool.shutdown();
	}
	public void configPlugin(Plugins me) {
		DruidPlugin druidPlugin = getDruidPlugin();
		wallFilter = new WallFilter();              // 加强数据库安全
		wallFilter.setDbType("mysql");
		druidPlugin.addFilter(wallFilter);
		druidPlugin.addFilter(new StatFilter());    // 添加 StatFilter 才会有统计数据
		me.add(druidPlugin);

		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setTransactionLevel(Connection.TRANSACTION_READ_COMMITTED);
		arp.setBaseSqlTemplatePath(PathKit.getRootClassPath());
		arp.addSqlTemplate("sql.sql");
		Engine engine=arp.getSqlKit().getEngine();
		engine.addSharedObject("StrKit",new StrKit());
		_MappingKit.mapping(arp);
		me.add(arp);



		//if(0==pr.getInt("devMode")){
			arp.setShowSql(true);
		//}


		me.add(new EhCachePlugin());

		//----文件服务器数据源配置
		DruidPlugin dsMysql = new DruidPlugin(p.get("jdbcFile"), p.get("userFile"), p.get("passwordFile").trim());
		me.add(dsMysql);
		ActiveRecordPlugin arpMysql = new ActiveRecordPlugin(CommonUtil.ZSHOP_FILE, dsMysql);
		me.add(arpMysql);
		arpMysql.addMapping("sys_files", Files.class);
		//----文件服务器数据源配置end


		//----zshop数据源配置start
		DruidPlugin zshopMysql = new DruidPlugin(p.get("zshopJdbcUrl"), p.get("user"), p.get("password").trim());
		me.add(zshopMysql);
		ActiveRecordPlugin zshopRecord = new ActiveRecordPlugin(CommonUtil.ZSHOP, zshopMysql);
		me.add(zshopRecord);
		_ZshopMappingKit.mapping(zshopRecord);
		//----zshop数据源配置end


		//任务调度
		//me.add(new Cron4jPlugin(PropKit.use("task.properties")));

		//缓存服务器
		//信息缓存
		RedisPlugin redisPlugin = new RedisPlugin("zshop", _Config.p.get("redis.host"), _Config.p.get("redis.password"));
		redisPlugin.getJedisPoolConfig().setMaxTotal(100);
		me.add(redisPlugin);

		//dubbo插件
		DubboPlugin dubboPlugin = new DubboPlugin();
		dubboPlugin.setApplication("dubbo_mall");
		RegistryConfig registry = new RegistryConfig();
		registry.setProtocol(p.get("dubbo.registry.protocol"));
		registry.setAddress(p.get("dubbo.registry.address"));
		dubboPlugin.setRegistry(registry);
		dubboPlugin.export(ApiInterface.class, new ApiService());//会员信息操作相关
		me.add(dubboPlugin);
		//dubbo插件

		//定时
		me.add(new Cron4jPlugin(PropKit.use("task.properties")));

	}

	public void configInterceptor(Interceptors me) {
		ExceptionInterceptor exceptionInterceptor=new ExceptionInterceptor();
		me.add(exceptionInterceptor);
	}

	public void configHandler(Handlers me) {

	}

	/**
	 * 本方法会在 jfinal 启动过程完成之后被回调，详见 jfinal 手册
	 */
	public void afterJFinalStart() {
		SerialNumberUtil.lastSnNumberInit();//初始化交易编号
		fixedThreadPool= Executors.newFixedThreadPool(50);

	}
}







